package com.csf.project.service.impl;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.annotation.Resource;
import javax.mail.BodyPart;
import javax.mail.MessagingException;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

import com.csf.project.service.ISendMailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * @Description:发邮件
 * @author: liangcm
 * @time:2017年4月20日 下午7:42:35
 */

@Service
@Transactional
public class SendMailServiceImpl implements ISendMailService {

	private static final Logger logger = LoggerFactory.getLogger(SendMailServiceImpl.class);

	@Resource
	JavaMailSender javaMailSender;

	@Value("${spring.mail.username}")private String fromEmail;

	/**
	 * 
	 * @Description:普通文本发邮件形式
	 * @param subject
	 * @param content
	 * @param toMail
	 *            void
	 * @exception:
	 * @author: liangcm
	 * @time:2017年4月20日 下午8:06:05
	 */
	@Override
	public void sendSimpleMail(String subject, String content, String toMail) {
		
		try {
			MimeMessage mailMessage = javaMailSender.createMimeMessage();
			MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage);
			messageHelper.setTo(toMail);

			messageHelper.setFrom(fromEmail);
			messageHelper.setSubject(subject);
			messageHelper.setText(content, false);
			javaMailSender.send(mailMessage);
			logger.info("邮件发送成功..");
		} catch (MessagingException e) {
			logger.error("邮件发失败..");
			logger.error("", e);
		}

	}
	

    /**
     * 带格式发送
     * @param subject
     * @param content
     * @param toMail
     */
	@Override
	public void sendHtmlMail(String subject, String content, String toMail) {
		try {
			 /* 创建代表组合MIME消息的MimeMultipart对象和该对象保存到的MimeBodyPart对象 */
	        MimeBodyPart mimeBodyPart = new MimeBodyPart();
	        // 创建一个MImeMultipart对象
	        MimeMultipart multipart = new MimeMultipart();
	        // 创建一个表示HTML正文的MimeBodyPart对象，并将它加入到前面的创建的MimeMultipart对象中
	        MimeBodyPart htmlBodyPart = new MimeBodyPart();
	        htmlBodyPart.setContent(content, "text/html;charset=UTF-8");
	        multipart.addBodyPart(htmlBodyPart);
	        // 将MimeMultipart对象保存到MimeBodyPart对象中
	        mimeBodyPart.setContent(multipart);

			MimeMessage mailMessage = javaMailSender.createMimeMessage();
			mailMessage.setContent(multipart);
			MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage);
			messageHelper.setTo(toMail);
			
			messageHelper.setFrom(fromEmail);
			messageHelper.setSubject(subject);
			javaMailSender.send(mailMessage);
			logger.info("邮件发送成功..");
		} catch (MessagingException e) {
			logger.error("邮件发失败..");
			logger.error("", e);
		}catch (Exception e) {
			logger.error("邮件发失败..");
			logger.error("", e);
		}

	}

    /**
     * 带附件发送邮件
     */
	@Override
	public void sendHtmlMailFiles(String subject, String content,
			String toMail, List<File> fileList,List<String> fileNameList) {
		try {
			  //创建代表组合MIME消息的MimeMultipart对象和该对象保存到的MimeBodyPart对象 
	        MimeBodyPart mimeBodyPart = new MimeBodyPart();
	        // 创建一个MImeMultipart对象
	        MimeMultipart multipart = new MimeMultipart();
	        // 创建一个表示HTML正文的MimeBodyPart对象，并将它加入到前面的创建的MimeMultipart对象中
	        MimeBodyPart htmlBodyPart = new MimeBodyPart();
	        htmlBodyPart.setContent(content, "text/html;charset=UTF-8");
	        multipart.addBodyPart(htmlBodyPart);
			try {
				if(fileList!=null){
					for (int i = 0; i < fileList.size(); i++) {
						BodyPart attachmentBodyPart = new MimeBodyPart();
						DataSource source = new FileDataSource(fileList.get(i));
						attachmentBodyPart.setDataHandler(new DataHandler(source));
						attachmentBodyPart.setFileName(MimeUtility.encodeText(MimeUtility.encodeText(fileNameList.get(i), "GBK", null)));
						multipart.addBodyPart(attachmentBodyPart);
					}
				}
						
			} catch (UnsupportedEncodingException e) {
				logger.error("发邮件读取附件失败",e);
			}
	        // 将MimeMultipart对象保存到MimeBodyPart对象中
	        mimeBodyPart.setContent(multipart);

			MimeMessage mailMessage = javaMailSender.createMimeMessage();
			mailMessage.setContent(multipart);
			MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage);
			messageHelper.setTo(toMail);
	
			messageHelper.setFrom(fromEmail);
			messageHelper.setSubject(subject);
			javaMailSender.send(mailMessage);
			logger.info("邮件发送成功..");
		} catch (MessagingException e) {
			logger.error("邮件发失败..");
			logger.error("", e);
		}
		

	}


}
